﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Text;

using ShopModel;

namespace ShopDAO
{
    public class DocumentDAO
    {
        private string connectionString;

        public DocumentDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// Zapisuje do bazy informacje o dokumencie dodanym do jednego z produktów (Saves information to the database about document added to the one of the products )
        /// </summary>
        /// <param name="Document">Objekt typu dokumnet przechowująci informacje o dodawanym dokumencie (Document object storing information about the document that is being added )</param>
        /// <returns>Jesli operacja tworzenia zakonczona sukcesem zwraca 'true' (Returns ‘true’ is operation succeeded)</returns>
        public  bool SaveDocument(DocumentInfo Document)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();

            sqlCommand.CommandText = "SaveDocument";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@FileName", Document.FileName));
            sqlCommand.Parameters.Add(new SqlParameter("@ProductID", Document.ProductID));

            try
            {
                connection.Open();
                return sqlCommand.ExecuteNonQuery() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Pobiera listę obiktów typu DocumentInfo dolączonych do produktu o podanym id (Downloads list of DocumentInfo objects enclosed to the product of given ID)
        /// </summary>
        /// <param name="ProductID">Unikalne id produktu (Unique produkt id)</param>
        /// <returns>Zwraca listę obiktów typu DocumentInfo przechowujących informacje o dolączoncyh dokumentach (Returns list of DocumentInfo storing information about enlosed documents)</returns>
        public  List<DocumentInfo> GetDocuments(int ProductID)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            List<DocumentInfo> result = new List<DocumentInfo>();

            command.CommandText = "GetDocuments";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@ProductID", ProductID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                while (sqlR.Read())
                {
                    result.Add(new DocumentInfo((int)sqlR["IDdocument"], (int)sqlR["ProductID"], (string)sqlR["FileName"]));
                }
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Usuwa z bazy dancyh pliki dokumentów dla produktu o podanym id (Deletes document’s files for the product of given ID from the database)
        /// </summary>
        /// <param name="DocumentID">Unikalne id produktu (Unique product id)</param>
        /// <returns>Jeśli operacja zakończona powodzeniem zwraca 'True' (Returns ‘true’ is operation succeeded)</returns>
        public  bool DeleteDocument(int DocumentID)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();

            sqlCommand.CommandText = "DeleteDocument";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@DocumentID", DocumentID));

            try
            {
                connection.Open();
                return (int)sqlCommand.ExecuteScalar() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }
        }


        /// <summary>
        /// Pobiera obikt typu DocumentInfo dolączony do produktu o podanym id (Downloads DocumentInfo object for the product of given ID)
        /// </summary>
        /// <param name="ProductID">Unikalne id produktu (Unique produkt id)</param>
        /// <returns>Zwraca obikt typu DocumentInfo przechowujący informacje o dolączonm dokumencie (Returns list of DocumentInfo storing information about enlosed documents)</returns>
        public DocumentInfo GetDocument(int DocumentID)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            DocumentInfo result = null;

            command.CommandText = "GetDocuments";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@DocumentID", DocumentID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if (sqlR.Read())
                    result = new DocumentInfo((int)sqlR["IDdocument"], (int)sqlR["ProductID"], (string)sqlR["FileName"]);
                
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Removes document’s files from the database.
        /// </summary>
        /// <param name="productID">Product id for which we remove document.</param>
        /// <returns>Returns True jesli when removal succeeded.</returns>
        public bool DeleteDocumentsFromDataBase(int productID)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();



            sqlCommand.CommandText = "DeleteDocumentsFromDataBase";
            sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
            sqlCommand.Parameters.Add(new SqlParameter("@productID", productID));



            try
            {
                connection.Open();
                sqlCommand.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }

        }
    }
}
